﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using SQLServerHealthCheck.Model;
using AG.Utilities;


namespace SQLServerHealthCheck.SQLServer.IssueTypes
{
    public class ServiceCheckMixedAuthenticationMode : IssueTypeCheckBase
    {

        public ServiceCheckMixedAuthenticationMode()
        {
            IssueTypeId = 2007;
        }

        public override void Check()
        {
            try
            {
                // Service check Mixed Authentication Mode
                HealthCheckDataContext dc = new HealthCheckDataContext(System.Configuration.ConfigurationManager.AppSettings["ConnectionStringSSHCDatabase"]);


                var Qry = from ss in dc.ServiceSettings
                          where (ss.Name == "MixedModeEnabled") && (Convert.ToInt32(ss.Value) == 1)


                          select ss;

                foreach (ServiceSetting serviceSetting in Qry)
                {
                    IssueType issueType = dc.IssueTypes.FirstOrDefault(i => i.IssueTypeId == IssueTypeId);

                    //Create new Issue

                    Issue issue = new Issue();
                    issue.IssueType = issueType;
                    issue.TargetId = serviceSetting.ServiceId;



                    issue.Description = issueType.Description.Replace("{InstanceName}", serviceSetting.Service.InstanceName);

                    dc.Issues.InsertOnSubmit(issue);
                }

                dc.SubmitChanges();
            }
            catch (Exception ex)
            {
                Logger.Error(ex);
            }

        }

    }
}
